const FILE_NOT_FOUND 		= "File not found. ";
const NO_PERMISSIONS 		= "No permisssions. ";
const READ_ERROR 			= "File read error. ";

const DELIMITER 			= '|';
const NL 					= "\n";


var IO = 
{
	filepath : function( filename )
	{
		//get extensions file path
		const id = "todotime@veinara.net";
		var extension = Components.classes["@mozilla.org/extensions/manager;1"]
							.getService(Components.interfaces.nsIExtensionManager)
							.getInstallLocation(id)
							.getItemLocation(id);
		return extension.path + ((extension.path.search(/\\/) != -1) ? "\\" : "/") + filename;
	},
	
	file_get_contents : function( filename )
	{
		try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		} catch (e) {
			Log.error( NO_PERMISSIONS + filename );
			return '';
		}
	
		var file = Components.classes["@mozilla.org/file/local;1"]
						.createInstance(Components.interfaces.nsILocalFile);
		file.initWithPath( IO.filepath( filename ) );
		if( ! file.exists( ) )
		{
			Log.warning( FILE_NOT_FOUND + filename );
			return '';
//			throw FILE_NOT_FOUND;
		}
		
		var is = Components.classes["@mozilla.org/network/file-input-stream;1"]
						.createInstance( Components.interfaces.nsIFileInputStream );

		try {
			is.init( file, 0x01, 00004, null );
		}
		catch ( e ) {
			Log.error( READ_ERROR + filename );
			return null;
		}
	
		var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
		                          .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
		converter.charset = /* The character encoding you want, using UTF-8 here */ "UTF-8";

		var scriptableStream = Components.classes["@mozilla.org/scriptableinputstream;1"]
		                                 .createInstance(Components.interfaces.nsIScriptableInputStream);

		scriptableStream.init(is);
		var chunk = scriptableStream.read(4096);
		try {			
			var input = converter.ConvertToUnicode(chunk);
		}
		catch( e ) {
			input = chunk;
		}
		return input;
	},
	
	
	file_put_contents : function( string, filename )
	{
		/*
		 //network
		var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
			.createInstance( Components.interfaces.nsIFileOutputStream );
		outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
		*/
		try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		} catch (e) {
			Log.error( NO_PERMISSIONS + filename );
			return false;
		}
		var file = Components.classes["@mozilla.org/file/local;1"]
						.createInstance(Components.interfaces.nsILocalFile);
	
		file.initWithPath( IO.filepath( filename ) );
		if( file.exists( ) == false )
		{
			file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
		}
		var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
								.createInstance( Components.interfaces.nsIFileOutputStream );
		outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
		
		var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
		                          .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
		converter.charset = "UTF-8";
		
		var chunk = null;
		try
		{
			chunk = converter.ConvertFromUnicode(string);
		}
		catch( e )
		{
			chunk = string;
		}
		outputStream.write( chunk, chunk.length );	
		
		 var fin = converter.Finish( );
		 if( fin.length > 0 )
		 {
		 	outputStream.write( fin, fin.length );
		 }
		 outputStream.close( );
		 return true;
	}
}